package com.wanmait.deliverymangementsystem.usercontroller;

import com.wanmait.deliverymangementsystem.pojo.Users;
import com.wanmait.deliverymangementsystem.service.UsersService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("user")
public class UserController {
    @Resource
    private UsersService usersService;
        //注册
    @GetMapping("register")
    public void register(){}
    @PostMapping("register")
    public String register(Users users){
        usersService.userRegister(users);
        return "redirect:/user/login";

    }
        //登录---用户
    @GetMapping("login")
    public void login(Model model,String error){
        if (error!=null){
            switch (error){
                case "1":
                    error="手机号或者密码错误";
                    break;
                case "2":
                    error="账号已冻结";
                    break;

            }
            model.addAttribute("error",error);
        }

    }
    @PostMapping("login")
    public String login(Users users, HttpSession session)
    {
        Users loginuser= usersService.userLogin(users);
        if (loginuser==null){

            return "redirect:/user/login?error=1";
        }
        else if (!loginuser.getEnable()){

            return "redirect:/user/login?error=2";
        }
        else
        {
            session.setAttribute("user",loginuser);
            usersService.lastlogintime(loginuser.getId());
            return "redirect:/user/index";
        }
    }
    //钱包
    @GetMapping("wallet")
    @ModelAttribute("money")
    public Double wallet(HttpSession session){
        Users users =(Users) session.getAttribute("user");
        Double money =users.getWallet();
        return money;
    }
    //充值
    @GetMapping("charge")
    public void charge(){

    }
    @PostMapping("charge")
    public String charge(Double money, HttpSession  session){
        Users user=(Users) session.getAttribute("user");
        Double wallet= user.getWallet();
        wallet+=money;
        user.setWallet(wallet);
        usersService.charge(user);
        session.setAttribute("user",user);
        return "redirect:/user/wallet";
    }
    @GetMapping("updateUserInfo")
    public void updateUserInfo(){

    }
    @RequestMapping("updateUserInfo")
    public String updateUserInfo(Users users,@SessionAttribute Users user,HttpSession session ){

        users.setId(user.getId());
        if (users.getEmail().isBlank()){
            users.setEmail(user.getEmail());
        }
        if (users.getPassword().isBlank()){
            users.setPassword(user.getPassword());
        }
        if (users.getPhone_number().isBlank())
        {
            users.setPhone_number(user.getPhone_number());
        }
        if (users.getUsername().isBlank()){
            users.setUsername(user.getUsername());
        }
        usersService.updateByID(users);
        session.invalidate();
        return "redirect:/user/login";
    }

}
